package com.practice.Optional语法糖;
import java.util.Optional;
/**
 * Created by IntelliJ IDEA2021.3
 * @Author: Tenghw
 * @Date: 2023/02/24  15:13
 * @Description:
 */
public class Demo2 {
    public static void main(String[] args) {
        Student student = new Student();
        Optional<Student> os1 = Optional.ofNullable(student);
        boolean present = os1.isPresent();
        System.out.println(present);
        System.out.println(student);
        System.out.println(os1.get());

        // 利用Optional的ifPresent方法做出如下：当student不为空的时候将student实例的name属性赋值为高启强
        Optional.ofNullable(student).ifPresent(p -> p.setName("高启强"));
        System.out.println(student);
        System.out.println(os1.get());
        // 如果无值
        Optional<Student> osl1 = Optional.ofNullable(null);
        osl1.ifPresent(p -> p.setName("高启盛"));
        System.out.println(osl1);
        //System.out.println(osl1.get());//会报异常
    }
}
